package com.crb.crm.manage.web.biz.msg;

import com.crb.crm.manage.entity.biz.MessageSendRecordSearchDTO;
import com.crb.crm.manage.entity.biz.MessageSendRecordVO;
import com.crb.crm.manage.entity.biz.TemplateSearchDTO;
import com.crb.crm.manage.service.biz.MessageSendRecordService;
import com.crb.crm.manage.service.biz.TemplateService;
import com.crb.crm.manage.utils.PageDataResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/messageSendRecord")
public class MessageSendRecordController {
	
	private static final Logger logger = LoggerFactory
			.getLogger(MessageSendRecordController.class);
	@Autowired
	private MessageSendRecordService messageSendRecordService;
	@Autowired
	private TemplateService templateService;
	@RequestMapping("/messageSendRecordList")
	public String toMessageSendRecordList(ModelMap map) {
		PageDataResult templates = templateService.getTemplates(new TemplateSearchDTO(), 1, Integer.MAX_VALUE);
		List<?> list = templates.getList();
		map.addAttribute("templateNames",list);
		return "biz/msg/messageSendRecordList";
	}
	
	/**
	 * 分页查询短消息发送记录列表
	 * @return ok/fail
	 */
	@RequestMapping(value = "/getMessageSendRecords", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions(value="getMessageSendRecords")
	public PageDataResult getMessageSendRecords(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, MessageSendRecordSearchDTO messageSendRecordSearch) {
		logger.debug("分页查询短消息发送记录列表！搜索条件：messageSendRecordSearch：" + messageSendRecordSearch + ",page:" + page
				+ ",每页记录数量limit:" + limit);
//		ErrorController
		PageDataResult pdr = new PageDataResult();
		try {
			if (null == page) {
				page = 1;
			}
			if (null == limit) {
				limit = 10;
			}
			// 获取短消息发送记录
			pdr = messageSendRecordService.getMessageSendRecords(messageSendRecordSearch, page, limit);
			logger.debug("短消息发送记录列表查询=pdr:" + pdr);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("短消息发送记录列表查询异常！", e);
		}
		return pdr;
	}

	/**
	 * 查询短消息发送记录数据
	 * @return map
	 */
	@RequestMapping(value = "/getMessageSendRecord", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getMessageSendRecord(@RequestParam("id") Integer id) {
		logger.debug("查询短消息发送记录数据！id:" + id);
		Map<String, Object> map = new HashMap<>();
		try {
			if (null == id) {
				logger.debug("查询短消息发送记录数据==请求参数有误，请您稍后再试");
				map.put("msg", "请求参数有误，请您稍后再试");
				return map;
			}
			// 查询商户
			MessageSendRecordVO messageSendRecordVO = messageSendRecordService.getMessageSendRecord(id);
			logger.debug("查询短消息发送记录数据！messageSendRecordVO=" + messageSendRecordVO);
			if (null != messageSendRecordVO) {
				map.put("messageSendRecord", messageSendRecordVO);
				map.put("msg", "ok");
			} else {
				map.put("msg", "查询商户信息有误，请您稍后再试");
			}
			logger.debug("查询短消息发送记录数据成功！map=" + map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "查询短消息发送记录数据错误，请您稍后再试");
			logger.error("查询短消息发送记录数据数据异常！", e);
		}
		return map;
	}
}
